//
//  NoticeController.m
//  Notice
//
//  Created by CPX on 2018/7/10.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "NoticeController.h"
#import "NoticeModel.h"
#import "NoticeCell.h"
#import "NewsDetailController.h"
#import "NoticeSetting.h"
//#import "UITableView+FDTemplateLayoutCell.h"

/**
 通知页
 @author yzl
 */
@interface NoticeController()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *table;
@property (nonatomic,strong) NSArray *list;
@property (nonatomic, strong) UIImageView *emptyView;

@end

@implementation NoticeController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self initUI];
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.table reloadData];
}

- (void)initUI{
    [self initNavigation];
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, NavHeight, kScreenWidth, kScreenHeight - NavHeight) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.rowHeight = UITableViewAutomaticDimension;
    self.table.estimatedRowHeight = 80;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.view.backgroundColor = UIColorHex(#f4f4f4);
    self.table.backgroundColor = UIColorHex(#f4f4f4);
    //    self.table.tableHeaderView = self.headerView;
    [self.view addSubview:self.table];
    
    MJRefreshNormalHeader *he = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    he.lastUpdatedTimeLabel.hidden = YES;
    he.stateLabel.hidden = YES;
    self.table.mj_header = he;
//    self.table.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.table.mj_header beginRefreshing];
    
    self.emptyView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"no_content_img"]];
    [self.view addSubview:self.emptyView];
    self.emptyView.center = CGPointMake(self.view.width/2, self.view.height/2);
    self.emptyView.hidden = YES;
}

- (void)initNavigation{
//    [self.navigationController setNavigationBarHidden:YES];
//
//    [self initLeftBackNaviButton];
//    [self.myNavigationBar.backButton setImage:nil forState:UIControlStateNormal];
//    [self.myNavigationBar.backButton setTitle:TLString(@"Notice") forState:UIControlStateNormal];
}

- (void)loadData{
    if (self.isSystemNotice) {
        [NoticeApi getSystemNoticeListWithSuccess:^(id response) {
            [self.table.mj_header endRefreshing];
            self.list = [NSArray yy_modelArrayWithClass:[NoticeModel class] json:response];
            for (NoticeModel *m  in self.list) {
                if (m.status == 2) {
                    if([NoticeSetting sharedSetting].readSystemNotis[@(m.notice_id)]){
                        m.status = 1;
                    }
                }
            }
            [self.table reloadData];
            self.emptyView.hidden = self.list.count != 0;
        } failure:^(NSError *error) {
            self.emptyView.hidden = self.list.count != 0;
            [MBProgressTool showTextForView:self.self.view text:error.userInfo[@"msg"]];
        }];
    }
    else{
        [NoticeApi getNoticeListWithSuccess:^(id response) {
            [self.table.mj_header endRefreshing];
            self.list = [NSArray yy_modelArrayWithClass:[NoticeModel class] json:response];
            [self.table reloadData];
            self.emptyView.hidden = self.list.count != 0;
        } failure:^(NSError *error) {
            self.emptyView.hidden = self.list.count != 0;
            [MBProgressTool showTextForView:self.self.view text:error.userInfo[@"msg"]];
        }];
    }
}

#pragma mark - table

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 56;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.list.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return [tableView fd_heightForCellWithIdentifier:@"identifer" cacheByIndexPath:indexPath configuration:^(id cell) {
//        NoticeCell *cell1 = cell;
//        cell1.notice = self.list[indexPath.row];
//    }];
//}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NoticeCell *cell = [NoticeCell cellWithTable:tableView];
    cell.notice = self.list[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NoticeModel *notice = self.list[indexPath.row];
    notice.status = 1;
    if ([notice.event_type isEqualToString:@"comment"]) {
        Class c = NSClassFromString(@"PrayViewController");
        if (c) {
            UIViewController *ctr = [c new];
            ctr.hidesBottomBarWhenPushed = YES;
            [ctr setValue:@(notice.event_id) forKey:@"bless_id"];
            [self.navigationController pushViewController:ctr animated:YES];
        }
        
    }
    else if ([notice.event_type isEqualToString:@"system_notify"]){
        [[NoticeSetting sharedSetting] addSystemNotice:notice.notice_id];
        NewsDetailController *ctr = [NewsDetailController new];
        ctr.notice = self.list[indexPath.row];
        [self.navigationController pushViewController:ctr animated:YES];
    }
    [NoticeApi updateNoticeRead:notice.notice_id success:^(id response) {
        
    } failure:^(NSError *error) {
        
    }];
}

@end
